package com.sojson.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.dao.AddressesDao;
import com.sojson.entity.po.Addresses;
import com.sojson.enums.EAddrIsDefault;
import com.sojson.service.AddressesService;
import com.sojson.util.token.TokenUtil;

/**
 * 收货地址表Service实现类
 * 
 * @author liu
 * @date 2021-03-19
 */
@Service
public class AddressesServiceImpl implements AddressesService {

    @Autowired
    private AddressesServiceImpl addressesServiceImpl;
    @Resource
    private AddressesDao addressesDaoImpl;

    /**
     * 单条插入
     * 
     * @param addresses
     * @return
     * @throws IOException 
     * @throws Exception 
     */
    @Override
    public synchronized int insert(Addresses addresses) throws IOException {
        return addressesServiceImpl.insertGo(addresses);
    }

    /**
     * 单条插入
     * 
     * @param addresses
     * @return
     * @throws IOException 
     * @throws Exception 
     */
    @Transactional
    public int insertGo(Addresses addresses) throws IOException {
        // 如果设为默认,就将当前用户下的其他地址设为非默认
        addressesServiceImpl.updateIsDefault(addresses);

        int insert = addressesDaoImpl.insert(addresses);
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(Addresses addresses) {
        int insertList = addressesDaoImpl.insertList(addresses.getAddressess());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(Addresses addresses) {
        int deleteByDto = addressesDaoImpl.deleteByDto(addresses);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = addressesDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<Addresses> findAll() {
        List<Addresses> findAll = addressesDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(Addresses addresses) {
        int countByDto = addressesDaoImpl.countByDto(addresses);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public List<Addresses> findByDto(Addresses addresses) {
        List<Addresses> findByDto = addressesDaoImpl.listByDto(addresses);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = addressesDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(ArrayList<Integer> ids) {
        int deleteByIdList = addressesDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(Addresses addresses) {
        int updateById = addressesDaoImpl.updateById(addresses);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(Addresses addresses) {
        int updateStaticByIdList = addressesDaoImpl.updateStaticByIdList(addresses);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(Addresses addresses) {
        int updateChangeByIdList = addressesDaoImpl.updateChangeByIdList(addresses.getAddressess());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param addresses
     * @return
     * @throws Exception 
     */
    @Override
    public int updatePartialById(Addresses addresses) throws IOException {
        // 如果设为默认,就将当前用户下的其他地址设为非默认
        addressesServiceImpl.updateIsDefault(addresses);

        int updatePartialById = addressesDaoImpl.updatePartialById(addresses);
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(Addresses addresses) {
        int updateStaticPartialByIdList = addressesDaoImpl.updateStaticPartialByIdList(addresses);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(Addresses addresses) {
        int updateChangePartialByIdList = addressesDaoImpl.updateChangePartialByIdList(addresses.getAddressess());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Addresses findById(Integer id) {
        Addresses findById = addressesDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Addresses> findByIdList(ArrayList<Integer> ids) {
        ArrayList<Addresses> findByIdList = addressesDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(Addresses addresses) {
        ArrayList<Integer> findIdByDto = addressesDaoImpl.findIdByDto(addresses);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        addressesDaoImpl.clear();
    }

    /**
     * 修改收货地址为非默认状态
     * @throws Exception 
     */
    @Transactional
    public void updateIsDefault(Addresses addresses) throws IOException {
        Integer isDefault = addresses.getIsDefault();
        if (isDefault != null && EAddrIsDefault.YES.getCode() == isDefault) {
            addressesDaoImpl.updateIsDefaultToNoByMId(TokenUtil.getToken().getMember().getId());
        }
    }

}